<!-- Global site tag (gtag.js) - Google Analytics -->
<script async="async" src="https://www.googletagmanager.com/gtag/js?id=YOUR ACCOUNT CODE"></script>
<script>
  //<![CDATA[
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date()); gtag('config', 'YOUR ACCOUNT CODE');

  window.addEventListener("load", enableAnalyticsEventCapture, false);

  function enableAnalyticsEventCapture() {
    // Download button
    $(document).on("click", ".btn-download", function() {
      var category = $(this).text();
      var label = getFileId($(this));
      gtag('event', 'Download',{'event_category' : category,
                                'event_label' : label});
    });

    // Request Access button
    $(document).on("click", ".btn-request", function() {
      var category = $(this).text();
      var label = getFileId($(this));
      gtag('event', 'Request Access', {'event_category' : category,
                                     'event_label' : label});
    });

    // Explore button
    $(document).on("click", ".btn-explore + ul.dropdown-menu li a", function() {
      var category = $(this).text();
      var label = getFileId($(this));
      gtag('event', 'Explore', {'event_category' : category,
                                'event_label' : label});
    });

    // Compute button
    $(document).on("click", ".btn-compute", function() {
      var category = $(this).text();
      var label = getFileId($(this));
      gtag('event', 'Compute', {'event_category' : category,
                                'event_label' : label});
    });

    // Preview button
    $(document).on("click", ".btn-preview", function() {
      var category = $(this).text();
      var label = getFileId($(this));
      gtag('event', 'Preview', {'event_category' : category,
                                'event_label' : label});
    });

    // Cite button
    $(document).on("click", ".downloadCitation + ul.dropdown-menu li a", function() {
      var category = $(this).text();
      var label = 'Unknown';
      if($('#fileForm').length > 0) {
        label = stripId($('#fileForm').attr('action'));
      } else if($('#datasetForm').length > 0) {
        label = stripId($('#datasetForm').attr('action'));
      }
      gtag('event', 'Download Citation', {'event_category' : category,
                                        'event_label' : label});
    });

    // Export Metadata button
    $(document).on("click", ".btn-export + ul.dropdown-menu li a", function() {
      var category = $(this).text();
      var label = 'Unknown';
      if($('#fileForm').length > 0) {
        label = stripId($('#fileForm').attr('action'));
      } else if($('#datasetForm').length > 0) {
        label = stripId($('#datasetForm').attr('action'));
      }
      gtag('event', 'Export Metadata', {'event_category' : category,
                                        'event_label' : label});
    });

    // Contact button
    $(document).on("click", ".btn-contact", function() {
      var category = $(this).text();
      var label = 'Unknown';
      if($('#fileForm').length > 0) {
        label = stripId($('#fileForm').attr('action'));
      } else if($('#datasetForm').length > 0) {
        label = stripId($('#datasetForm').attr('action'));
      } else if($('#dataverseForm').length > 0) {
        label = stripAlias($('#dataverseForm').attr('action'));
      }
      gtag('event', 'Contact', {'event_category' : category,
                                'event_label' : label});
    });

    // Share button
    $(document).on("click", 'div[id$="shareDialog"] a[class^="sharrre"]', function() {
      var sharrreVal = $(this).attr('class');
      var category = sharrreVal.substring(sharrreVal.indexOf('-') + 1) ;
      var label = 'Unknown';
      if($('#fileForm').length > 0) {
        label = stripId($('#fileForm').attr('action'));
      } else if($('#datasetForm').length > 0) {
        label = stripId($('#datasetForm').attr('action'));
      } else if($('#dataverseForm').length > 0) {
        label = stripAlias($('#dataverseForm').attr('action'));
      }
      gtag('event', 'Share', {'event_category' : category,
                              'event_label' : label});
    });

  }

  function getFileId(target) {
    var label = 'Unknown';
    if(target.parents('th').length>0) {
      //Special case - the Download button that downloads all selected files in the dataset
      label = 'file(s) from ' + stripId($('#datasetForm').attr('action'));
    } else {
      var row = target.parents('tr')[0];
      if(row != null) {
        //finds the file id/DOI in the Dataset page
        label = $(row).find('td.col-file-metadata > a').attr('href');
      } else {
        //finds the file id/DOI in the file page
        label = $('#fileForm').attr('action');
      }
      label = stripId(label);
    }
    return label;
  }


  function stripId(label) {
    var index = label.indexOf('&');
    if(index == -1) index = label.length;
    if(label.indexOf('persistentId=') >= 0) {
      label = label.substring(label.indexOf('persistentId=') + 13, index);
    }
    if(label.indexOf('fileId=') >= 0) {
      label = label.substring(label.indexOf('fileId=') + 7, index);
    }
    return(label);
  }

  function stripAlias(label) {
    var index = label.length;
    label = label.substring(label.indexOf('/dataverse/') + 11, index).split(/[?#]/)[0];
    return(label);
  }
  //]]>
</script>
